// test/pages/trainingSign/trainingSign.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    namefocus:false,
    phonefocus:false,
    name:'',
    phone:'',
    phoneInvalid:false,
    nameInvalid:false
  },
  getData(id){
    const that = this
    wx.request({
      url: app.globalData.host+'Train/train_details',
      data:{
        id,
        token:wx.getStorageSync('token')||''
      },
      method:'POST',
      success(res){
        console.log(res)
        if(res.data.code==2000){
          that.setData({
            dataList:res.data.data
          })
        }else{
          wx.showToast({
            title: res.data.msg,
            icon:'none'
          })
        }
      },
      fail(err){
        console.log(err)
      }
    })
  },
  checkform(){
    if(this.data.name.trim().length<2||this.data.name.trim().length>12){
      this.setData({namefocus:true,nameInvalid:true})
      return false
    }else if(this.data.phone.trim().length!=11){
      this.setData({phonefocus:true,phoneInvalid:true})
      return false
    }
    return true
  },
  signTrain(){
    const that = this
    if(that.checkform()){
      wx.request({
      url: app.globalData.host+'Train/wxpay',
      data:{
        train_id:parseInt(that.data.id),
        token:wx.getStorageSync('token'),
        name:that.data.name,
        phone:that.data.phone,
        img:wx.getStorageSync('userInfo').avatarUrl||''
      },
      method:'POST',
      success(res){
        console.log(res)
        if(res.data.code==2000){
          let data =res.data.data
          wx.requestPayment({
            nonceStr: data.nonceStr,
            package: data.package,
            paySign: data.paySign,
            timeStamp: data.timeStamp,
            signType:data.signType,
            success(res){
              console.log(res)
              wx.showToast({
                title: '支付成功',
              })
              setTimeout(() => {
                wx.navigateTo({
                  url: '/test/pages/trainingDetail/trainingDetail?id='+that.data.id,
                })
              }, 1000);
            },
            fail(err){
              wx.showToast({
                title: '支付失败',
                icon:'none'
              })
            }
          })
        }
      }
    })
    }
  },
  namefocus(){
    this.setData({namefocus:true})
  },
  phonefocus(){
    this.setData({phonefocus:true})
  },
  nameblur(){
    if(this.data.name.trim().length<2||this.data.name.trim().length>12){
      wx.showToast({
        title: '姓名长度在2-12个字符之间',
        icon:'none'
      })
      this.setData({
        nameInvalid:true
      })
    }else{
      this.setData({
        nameInvalid:false
      })
    }
    this.setData({namefocus:false})
  },
  phoneblur(){
    if(this.data.phone.trim().length<11){
      wx.showToast({
        title: '联系电话只能是11位手机号码',
        icon:'none'
      })
      this.setData({
        phoneInvalid:true
      })
    }else{
      this.setData({
        phoneInvalid:false
      })
    }
    this.setData({phonefocus:false})
  },
  setname(e){
    this.setData({name:e.detail.value})
  },
  setphone(e){
    this.setData({phone:e.detail.value})
  },
  call(){
    wx.makePhoneCall({
      phoneNumber: this.data.dataList.phone,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getData(options.id)
    this.setData({
      id:options.id,
      fitFoot:wx.getStorageSync('safeArea')
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})